package cn.xutongxue.interview.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.xutongxue.interview.entity.Question;
import cn.xutongxue.interview.entity.QuestionBankMap;
import cn.xutongxue.interview.entity.User;
import cn.xutongxue.interview.entity.vo.QuestionVo;
import cn.xutongxue.interview.entity.vo.bank.BankDetailVo;
import cn.xutongxue.interview.enums.Errors;
import cn.xutongxue.interview.exception.BusinessException;
import cn.xutongxue.interview.mapper.QuestionBankMapMapper;
import cn.xutongxue.interview.mapper.QuestionMapper;
import cn.xutongxue.interview.service.QuestionBankMapService;
import cn.xutongxue.interview.service.QuestionService;
import cn.xutongxue.interview.utils.ThrowUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.xutongxue.interview.entity.QuestionBank;
import cn.xutongxue.interview.service.QuestionBankService;
import cn.xutongxue.interview.mapper.QuestionBankMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【question_bank(题库表)】的数据库操作Service实现
* @createDate 2024-09-11 16:43:43
*/
@Service
public class QuestionBankServiceImpl extends ServiceImpl<QuestionBankMapper, QuestionBank>
    implements QuestionBankService{

    @Resource
    private QuestionBankMapService questionBankMapService;

    @Resource
    private QuestionService questionService;

    @Override
    public BankDetailVo queryBankDetailById(Long id) {
        BankDetailVo bankDetailVo = new BankDetailVo();
        // 查询题库详情
        QuestionBank questionBank = getById(id);
        LambdaQueryWrapper<QuestionBankMap> mapWrapper = Wrappers.lambdaQuery(QuestionBankMap.class)
                .eq(QuestionBankMap::getBankId, id);
        // 从关联表查询关联题目
        List<QuestionBankMap> questionBankMaps = questionBankMapService.list(mapWrapper);
        // 复制题库信息到Vo
        BeanUtil.copyProperties(questionBank, bankDetailVo);
        if (CollUtil.isNotEmpty(questionBankMaps)) {
            // 如果关联题目不为空
            // 获取题目id集合
            Set<Long> idSet = questionBankMaps.stream().map(QuestionBankMap::getQuestionId).collect(Collectors.toSet());
            // 查询题目列表
            List<Question> questionList = questionService.list(Wrappers.lambdaQuery(Question.class).in(Question::getId, idSet));
            // 复制题目信息到Vo
            bankDetailVo.setQuestionList(questionList.stream().map(QuestionVo::objToVo).collect(Collectors.toList()));
            // 设置题目题库总数
            bankDetailVo.setQuestionTotal(questionList.size());
        } else {
            // 如果为空则设置为0
            bankDetailVo.setQuestionTotal(0);
            bankDetailVo.setQuestionList(Collections.emptyList());
        }
        return bankDetailVo;
    }

    @Override
    public void addSingleQuestionToBank(Long bankId, Long questionId) {
        // 参数校验
        if (bankId < 0 || questionId < 0) {
            throw new BusinessException(Errors.PARAMS_ERROR);
        }
        User currentUser = (User) StpUtil.getLoginIdDefaultNull();
        QuestionBankMap questionBankMap = new QuestionBankMap();
        questionBankMap.setQuestionId(questionId);
        questionBankMap.setBankId(bankId);
        questionBankMap.setUserId(currentUser.getId());
        ThrowUtils.throwIf(!questionBankMapService.save(questionBankMap), Errors.OPERATION_ERROR, "添加题目到题库失败");
    }
}




